/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ad.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.common.shiro.realm.LoginInfo;
import com.jeesite.modules.ad.dao.AdDao;
import com.jeesite.modules.ad.entity.Ad;
import com.jeesite.modules.ad.entity.AdAgent;
import com.jeesite.modules.ad.entity.AdGuest;
import com.jeesite.modules.ad.entity.AdVertiser;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.sys.dao.UserRoleDao;
import com.jeesite.modules.sys.utils.UserUtils;

/**
 * 广告Service
 * 
 * @author chenxm
 * @version 2019-04-17
 */
@Service
@Transactional(readOnly = true)
public class AdService extends CrudService<AdDao, Ad> {

	@Autowired
	private UserRoleDao userRoleDao;

	@Autowired
	private AdVertiserService adVertiserService;

	@Autowired
	private AdAgentService adAgentService;

	@Autowired
	private AdDao adDao;

	@Autowired
	private AdGuestService adGuestService;

	/**
	 * 获取单条数据
	 * 
	 * @param ad
	 * @return
	 */
	@Override
	public Ad get(Ad ad) {
		return super.get(ad);
	}

	/**
	 * 查询分页数据
	 * 
	 * @param ad 查询条件
	 * @param    ad.page 分页对象
	 * @return
	 */
	@Override
	public Page<Ad> findPage(Ad ad) {
		LoginInfo loginInfo = UserUtils.getLoginInfo();
		AdVertiser curVertiser = this.adVertiserService.getByUserCode(loginInfo.getId());
		String where = null;
		if (curVertiser != null) {
			ad.setVertiserId(curVertiser.getId());
		} else {
			AdAgent adAgent = adAgentService.getCurAgent();
			if (adAgent != null) {
				where = " AND u10.agent_Id = '" + adAgent.getId() + "'";
			}
		}
		if (!StringUtils.isBlank(ad.getAdVertiser().getAccount())) {
			if (where == null) {
				where = " AND u10.account LIKE '%" + ad.getAdVertiser().getAccount() + "%'";
			} else {
				where = where + " AND u10.account LIKE '%" + ad.getAdVertiser().getAccount() + "%'";
			}
		}
		ad.getSqlMap().put("data_power", where);
		return super.findPage(ad);
	}

	/**
	 * 保存数据（插入或更新）
	 * 
	 * @param ad
	 */
	@Override
	@Transactional(readOnly = false)
	public void save(Ad ad) {
		// 修改广告计划如果有获客信息，则无法修改
		if (!ad.getIsNewRecord()) {
			AdGuest where = new AdGuest();
			where.setAd(new Ad(ad.getId()));
			if (adGuestService.findCount(where) > 0) {
				throw newValidationException("该广告计划存有获客信息,无法修改");
			}
		}
		if (StringUtils.isEmpty(ad.getVertiserId())) {
			AdVertiser adVertiser = adVertiserService.getCurVertiser();
			ad.setVertiserId(adVertiser.getId());
		}
		super.save(ad);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(ad.getId(), "ad_image");
		// 保存上传附件
		FileUploadUtils.saveFileUpload(ad.getId(), "ad_file");
	}

	/**
	 * 更新状态
	 * 
	 * @param ad
	 */
	@Override
	@Transactional(readOnly = false)
	public void updateStatus(Ad ad) {
		if (ad != null) {
			AdGuest where = new AdGuest();
			where.setAd(new Ad(ad.getId()));
			if (adGuestService.findCount(where) > 0) {
				throw newValidationException("该广告计划存有获客信息,无法停用");
			}
		}
		super.updateStatus(ad);
	}

	/**
	 * 删除数据
	 * 
	 * @param ad
	 */
	@Override
	@Transactional(readOnly = false)
	public void delete(Ad ad) {
		if (ad != null) {
			AdGuest where = new AdGuest();
			where.setAd(new Ad(ad.getId()));
			if (adGuestService.findCount(where) > 0) {
				throw newValidationException("该广告计划存有获客信息,无法删除");
			}
		}
		super.delete(ad);
	}

}